//
//  CLBlueManger.h
//  CLBlueTool
//
//  Created by MYO on 16/3/23.
//  Copyright © 2016年 MYO. All rights reserved.
//
// @see https://github.com/jsonsnow/CoreBlue

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "CBPeripheral+YYConnectedPeripheral.h"

FOUNDATION_EXPORT  NSString *const BlueCharacteristicRead;
FOUNDATION_EXPORT  NSString *const BlueCharacteristicWrite;
FOUNDATION_EXPORT  NSString *const kBlueManagerDidConnect;
FOUNDATION_EXPORT  NSString *const kBlueManagerResetNotifaiton;

typedef NS_ENUM(NSUInteger, CLBlueConnectState) {
    
    CLBlueConnectStateDefault = 0,
    CLBlueConnectStateConnecting,
    CLBlueConnectStateConnected,
    CLBlueConnectStateCutConnect,
    CLBlueConnectStateFailedConnect,
    CLBlueConnectStateCancel
};

typedef NS_OPTIONS(NSUInteger, CLBlueManagerState) {
    
    CLBlueManagerStateDefault = 0,
    CLBlueManagerStatePoweredOn,
    CLBlueManagerStateStateUnknown,
    CLBlueManagerStateUnsupported,
    CLBlueManagerStateUnauthorized,
    CLBlueManagerStatePoweredOff,
    CLBlueManagerStateResetting
};
@class CLBlueManger;

@protocol  CLBlueMangerInterrput <NSObject>

@optional;
- (BOOL)manager:(CLBlueManger *)manager willConnetPeripheral:(CBPeripheral *)peripheral;
- (void)manager:(CLBlueManger *)manager didConnetPeripheral:(CBPeripheral *)peripheral;

@end
@protocol CLBlueMangerDelegate <NSObject>

@required

/**
 *  扫描蓝牙设备时，当搜索到新的蓝牙设备时候执行，一般在这个回调中进行对应外设的连接
 *
 *  @param manager self
 *  @param peripheral 搜索到的新蓝牙设备
 */
-(void)blueManagerblescanBluDevice:(CLBlueManger *)manager andTheDevice:(CBPeripheral *)peripheral;

/**
 连接成功的时候外设需要进行查找的服务地址

 @param manager    self
 @param peripheral 所连接的外设

 @return 该外设存在的服务地址
 */
-(NSArray *)blueManagerServerAdrres:(CLBlueManger *)manager peripheral:(CBPeripheral *)peripheral;

/**
 服务对应的特性值，特性值分write 和 read 存在返回的字典中，分别以BlueCharacteristicWrite 和 BlueCharacteristicRead指定
 
 @see BlueCharacteristicWrite
 @see BlueCharacteristicRead

 @param manager    self
 @param peripheral peripheral

 @return 特性值地址
 */
-(NSDictionary *)blueManagerCharacteristicsAdrres:(CLBlueManger *)manager peripheral:(CBPeripheral *)peripheral;


/**
 外设连接成功

 @param manager    self
 @param peripheral peripheral
 */
-(void)blueManagerdidConnectSuccees:(CLBlueManger *)manager peripheral:(CBPeripheral *)peripheral;


/**
 外设断开连接

 @param manager    self
 @param peripheral peripheral
 @param error      error
 */
-(void)blueManagerCutConnect:(CLBlueManger *)manager peripheral:(CBPeripheral *)peripheral error:(NSError *)error;


/**
 外设连接失败

 @param manager    self
 @param peripheral peripheral
 @param error      error
 */
-(void)blueManagerFailedConnect:(CLBlueManger *)manager peripheral:(CBPeripheral *)peripheral error:(NSError *)error;

@optional

- (void)blueManagerPoweroff:(CLBlueManger *)manager status:(CLBlueManagerState)state;

-(void)blueManagerdidWriteError:(CLBlueManger *)manager peripheral:(CBPeripheral *)peripheral error:(NSError *)error;
/**
 *  回传用户手机的蓝牙状态
 *
 *  @param manager self
 *  @param status  蓝牙状态
 */

-(void)blueManagerbleStausForCurerntPhone:(CLBlueManger *)manager andTheStauts:(CLBlueManagerState)status;


/**
 这个方法当手机无法正常开启蓝牙的时候调用，建议实现来提示用户检测蓝牙设置,-(void)blueManagerbleStausForCurerntPhone:(CLBlueManger *)manager andTheStauts:(CLBlueManagerState)status该方法回调所有状态

 @param manager self
 @param statu   statu;
 */
-(void)blueManagerFailedState:(CLBlueManger *)manager statu:(CLBlueManagerState)statu;

/**
 *  接收到蓝牙设备发送的数据时候执行
 *
 *  @param manager self
 *  @param data    蓝牙设备发送回来的数据
 */

-(void)blueManagerblereceiveData:(CLBlueManger *)manager andTheData:(NSData *)data;


@end

@interface CLBlueManger : NSObject

@property (nonatomic, weak) id<CLBlueMangerDelegate> delegate;
@property (nonatomic, weak) id<CLBlueMangerInterrput> interrput;
@property (nonatomic, assign,readonly) BOOL needMacAddress;
@property (nonatomic, assign, readonly) CLBlueConnectState connectState;
@property (nonatomic, assign, readonly) CLBlueManagerState managerState;
@property (nonatomic, assign) BOOL needSelf;

-(instancetype)init UNAVAILABLE_ATTRIBUTE;
+(instancetype)new UNAVAILABLE_ATTRIBUTE;

+(instancetype)sharedManger;

/**
 *  扫描蓝牙设备
 */
-(void)scan;

///停止搜索
-(void)stop;



- (void)searchDeviceBySpeicfeName:(NSString *)name block:(void(^)(NSArray *array))block;
- (void)stopSearchSameNameDevice;

- (void)connect:(CBPeripheral *)peripheral orTheDeviceName:(NSString *)name;

- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

- (void)resetConnet:(CBPeripheral *)peripheral;

- (void)removeAllDevice;


/**
 写数据

 @param data        data
 @param writeHandle nil(通过delegate回调)
 */
-(void)writeDataToDevice:(NSData *)data writeHandle:(void(^)(NSError *error))writeHandle;



@end
